﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DeJC.BLL;
using DeJC.BLL.Managers;
using DeJC.DAL.Model;
using TheList.Helper;

namespace TheList.Controllers
{ 
    public class ProductsController : Controller
    {
        private TheListEntities db = new TheListEntities();
        //private UnitOfWork unitOfWork = new UnitOfWork();
        //
        // GET: /Products/

        public ViewResult Index()
        {
            IEnumerable<Product> products;
                products=ProductManager.GetProducts();    
            return View(products);
        }

        //
        // GET: /Products/Details/5

        public ViewResult Details(int id)
        {
            Product product = db.Products.Find(id);
            return View(product);
        }

        //
        // GET: /Products/Create

        public ActionResult Create()
        {
            ViewBag.CategoryID = new SelectList(GeneralMiscTypeManager.GetChildren(Utility.ParentCategoryId), "ID", "Name");
            ViewBag.ProductBrandID = new SelectList(GeneralMiscTypeManager.GetChildren(Utility.ParentProductBrandId), "ID", "Name");
            ViewBag.UnitID=new SelectList(GeneralMiscTypeManager.GetChildren(Utility.ParentProductUnitId), "ID", "Name");
            return View();
        } 

        //
        // POST: /Products/Create

        [HttpPost]
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.CategoryID=new SelectList(GeneralMiscTypeManager.GetChildren(Utility.ParentCategoryId), "ID", "Name",product.CategoryID);
            ViewBag.ProductBrandID=new SelectList(GeneralMiscTypeManager.GetChildren(Utility.ParentProductBrandId), "ID", "Name",product.ProductBrandID);
            ViewBag.UnitID=new SelectList(GeneralMiscTypeManager.GetChildren(Utility.ParentProductUnitId), "ID", "Name",product.UnitID);
            return View(product);
        }
        
        //
        // GET: /Products/Edit/5
 
        public ActionResult Edit(int id)
        {
            Product product = db.Products.Find(id);
            ViewBag.CategoryID = new SelectList(db.GeneralMiscTypes, "ID", "Name", product.CategoryID);
            ViewBag.ProductBrandID = new SelectList(db.GeneralMiscTypes, "ID", "Name", product.ProductBrandID);
            ViewBag.UnitID = new SelectList(db.GeneralMiscTypes, "ID", "Name", product.UnitID);
            return View(product);
        }

        //
        // POST: /Products/Edit/5

        [HttpPost]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryID = new SelectList(db.GeneralMiscTypes, "ID", "Name", product.CategoryID);
            ViewBag.ProductBrandID = new SelectList(db.GeneralMiscTypes, "ID", "Name", product.ProductBrandID);
            ViewBag.UnitID = new SelectList(db.GeneralMiscTypes, "ID", "Name", product.UnitID);
            return View(product);
        }

        //
        // GET: /Products/Delete/5
 
        public ActionResult Delete(int id)
        {
            Product product = db.Products.Find(id);
            return View(product);
        }

        //
        // POST: /Products/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Product product = db.Products.Find(id);
            db.Products.Remove(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            //unitOfWork.Dispose();
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}